package com.example.yls.demoa;

import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private Button btnstar;
    private Button btnPause;
    private Button btnstop;
    private MediaPlayer mediaPlayer;
    private SeekBar btnSeekbar;
    private TextView txtcur;
    private TextView txtxtotal;
    private Handler handler;
    private int MSG_PROGRESS_CHANGRD=1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnstar=findViewById(R.id.btn_star);
        btnPause=findViewById(R.id.btn_pause);
        btnstop=findViewById(R.id.btn_stop);
        btnSeekbar=findViewById(R.id.btn_seekbar);
        txtcur=findViewById(R.id.curtime);
        txtxtotal=findViewById(R.id.totaltime);
        /*通过消息机制获取时长*/
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == MSG_PROGRESS_CHANGRD) {
                    int position = message.arg1;
                int total = message.arg2;
                txtcur.setText(String.valueOf(position));
                txtcur.setText(String.valueOf(total));
            }
                return false;
            }
        });
        /*获取进度条位置*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                    int position=mediaPlayer.getCurrentPosition();
                    int total=mediaPlayer.getDuration();
                    int progress=100*position/total;
                    btnSeekbar.setProgress(progress);
                    Message message=new Message();
                    message.what=MSG_PROGRESS_CHANGRD;
                    message.arg1=position;
                    message.arg2=total;
                    handler.sendMessage(message);
                }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        /*拖动播放*/
        btnSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer!=null){
                  int progress=seekBar.getProgress();
                  int total=mediaPlayer.getDuration();
                  int position=total*progress/100;
                  mediaPlayer.seekTo(position);

                }

            }
        });
        btnstar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               if (mediaPlayer==null) {
                   mediaPlayer = new MediaPlayer();
                   try {
                       mediaPlayer.setDataSource("/mnt/shared/Other/shangfeng.aac");
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
                   mediaPlayer.prepareAsync();
                   mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                       @Override
                       public void onPrepared(MediaPlayer mediaPlayer) {
                           mediaPlayer.start();
                       }
                   });
               }else {
                   mediaPlayer.start();
               }

            }
        });
        btnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                mediaPlayer.pause();

            }}
        });
        btnstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mediaPlayer.stop();

            }
        });
    }
}
